﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//   悔棋
namespace PreventChess
{
    public partial class FrmChessBoard : Form
    {

        #region 全局变量

        const int DPI = 1;//分辨率系数

        public int WINDOW_WIDTH = 1080 * DPI; //窗口尺寸
        public int WINDOW_HEIGHT = 720 * DPI;

        public int BORDER_WIDTH = 40 * DPI;   //棋盘边缘宽度 
        public int BLOCK_SIZE = 50 * DPI;   //棋盘格子长宽 
        public int BLOCK_NUMBER = 4; //棋盘格子数 N*N
        public int CONTROL_BTN_WIDTH = 60;//控制按钮尺寸

        public int PLAYER_NUMBER = 2; //玩家数量

        public int CHESS_BUTTON_SIZE = 80 * DPI;  //棋子按钮长宽
        public int CHESSMAN_AMOUNT = 6;  //每位玩家棋子数量

        public static int ARROW_PEN_WIDTH = 8; //棋盘内棋子 箭头的宽度

        public bool IS_MOVE_LIMIT = false;
        public bool IS_BORDER_ACROSS = false;

        public int active_player = 1;

        #endregion


        #region 全局控件

        public static ChessmanButton active_chess_btn; //操作界面选中的棋子按钮

        BoardChess active_chess; // 棋盘中选中的棋子
        Panel chess_panel; // 棋盘
        Panel operate_panel; //棋盘右侧操作界面
        ChessBoardBlock checked_chess_panel = new ChessBoardBlock(); // 用于遮罩棋盘中选中的棋子 

        Label label_player = new Label(); //显示当前玩家
        Label label_tip = new Label();  //显示提示

        BoardChess temp_chess; // 点击棋盘生成的临时棋子
        Button confirm_btn; //确定下棋按钮
        Button turn_around_btn; //旋转棋子按钮
        Button cancel_btn; //取消下棋按钮

        Button backoff_btn = new Button();
        Label label_player_number = new Label();

        #endregion


        #region 全局控件组

        Dictionary<int, ChessmanButton> red_chess_button_dic = new Dictionary<int, ChessmanButton>();
        Dictionary<int, ChessmanButton> orange_chess_button_dic = new Dictionary<int, ChessmanButton>();
        Dictionary<int, ChessmanButton> yellow_chess_button_dic = new Dictionary<int, ChessmanButton>();
        Dictionary<int, ChessmanButton> green_chess_button_dic = new Dictionary<int, ChessmanButton>();
        Dictionary<int, ChessmanButton> blue_chess_button_dic = new Dictionary<int, ChessmanButton>();
        Dictionary<int, ChessmanButton> purple_chess_button_dic = new Dictionary<int, ChessmanButton>();

        Dictionary<Point, BoardChess> board_map_dic = new Dictionary<Point, BoardChess>(); //储存棋盘上的棋子
        Dictionary<int, BoardChess> last_move_chess_dic = new Dictionary<int, BoardChess>(); //不能连续两回合移动同一个棋子 储存上一回合移动的棋子

        Dictionary<Point,ChessBoardBlock> target_block_dic = new Dictionary<Point,ChessBoardBlock>(); //储存棋子可走的目标格子

        List<int> alive_player_list = new List<int>(); //存活的玩家
        #endregion


        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmChessBoard()
        {
            InitializeComponent();
            // FrmNewGameSet frmNewGameSet = new FrmNewGameSet();
            this.Width = WINDOW_WIDTH;
            this.Height = WINDOW_HEIGHT;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;


        }


        /// <summary>
        /// 已有对局的时候，开始新游戏时要重置变量
        /// </summary>
        private void RestarGameInit()
        {
            if (this.Controls.Contains(chess_panel))//要先销毁原来的
            {
                chess_panel.Controls.Clear();
                this.Controls.Remove(chess_panel);
                chess_panel.Dispose();
            }

            if (this.Controls.Contains(operate_panel))
            {
                operate_panel.Controls.Clear();
                this.Controls.Remove(operate_panel);
                operate_panel.Dispose();
            }

            red_chess_button_dic.Clear();
            orange_chess_button_dic.Clear();
            yellow_chess_button_dic.Clear();
            green_chess_button_dic.Clear();
            blue_chess_button_dic.Clear();
            purple_chess_button_dic.Clear();

            board_map_dic.Clear();
            target_block_dic.Clear();

            last_move_chess_dic.Clear();

            alive_player_list.Clear();

            active_player = 1;
            active_chess = null;
         
        }


        #region 绘制主界面

        /// <summary>
        /// 绘制棋盘
        /// </summary>
        public void DrawChessBoard()
        {
            RestarGameInit();

            CHESSMAN_AMOUNT = (int)(Math.Floor((double)(BLOCK_NUMBER * (BLOCK_NUMBER - 1) / PLAYER_NUMBER)));//计算每个玩家棋子数量

            switch (BLOCK_NUMBER)
            {
                case 4:
                    BLOCK_SIZE = 120 * DPI;
                    ARROW_PEN_WIDTH = 8;
                    break;
                case 5:
                    BLOCK_SIZE = 96 * DPI;
                    ARROW_PEN_WIDTH = 7;

                    break;
                case 6:
                    BLOCK_SIZE = 80 * DPI;
                    ARROW_PEN_WIDTH = 6;

                    break;
                case 7:
                    BLOCK_SIZE = 69 * DPI;
                    ARROW_PEN_WIDTH = 5;

                    break;
                case 8:
                    BLOCK_SIZE = 60 * DPI;
                    ARROW_PEN_WIDTH = 4;

                    break;
                default:
                    break;
            }


            chess_panel = new Panel();
            chess_panel.Width = BLOCK_NUMBER * BLOCK_SIZE + BORDER_WIDTH * 2;
            chess_panel.Height = chess_panel.Width;

            this.Height = chess_panel.Height + 65; //窗口高度适应棋盘
            WINDOW_HEIGHT = this.Height;
            this.Width = WINDOW_WIDTH;

            chess_panel.Location = new Point(1, 25);
            chess_panel.BackgroundImage = Properties.Resources.back;
            chess_panel.BackgroundImageLayout = ImageLayout.Stretch;
            chess_panel.Paint += this.chess_panel_Paint;
            chess_panel.MouseClick += this.chess_panel_MouseClick;

            this.Controls.Add(chess_panel);


            temp_chess = new BoardChess();//  用于棋子确定之前暂时显示棋子方向
            temp_chess.Width = (int)(BLOCK_SIZE / 1.2);
            temp_chess.Height = temp_chess.Width;
            chess_panel.Controls.Add(temp_chess);
            temp_chess.Visible = false;


            confirm_btn = new Button(); //确定
            //confirm_btn.AutoSize = true;
            confirm_btn.Text = "";
            confirm_btn.Width = CONTROL_BTN_WIDTH;
            confirm_btn.Height = CONTROL_BTN_WIDTH;
            confirm_btn.FlatStyle = FlatStyle.Flat;//样式
            confirm_btn.ForeColor = Color.Transparent;//前景
            confirm_btn.BackColor = Color.Transparent;//去背景
            confirm_btn.FlatAppearance.BorderSize = 0;//去边线
            confirm_btn.BackgroundImage = Properties.Resources.confirm;
            confirm_btn.BackgroundImageLayout = ImageLayout.Zoom;
            confirm_btn.Click += this.confirm_Click;
            chess_panel.Controls.Add(confirm_btn);
            confirm_btn.Visible = false;


            turn_around_btn = new Button(); //棋子位置确定之前 旋转棋子
            turn_around_btn.Text = "";
            //turn_around_btn.AutoSize = true;
            turn_around_btn.Width = CONTROL_BTN_WIDTH;
            turn_around_btn.Height = CONTROL_BTN_WIDTH;
            turn_around_btn.FlatStyle = FlatStyle.Flat;//样式
            turn_around_btn.ForeColor = Color.Transparent;//前景
            turn_around_btn.BackColor = Color.Transparent;//去背景
            turn_around_btn.FlatAppearance.BorderSize = 0;//去边线
            turn_around_btn.BackgroundImage = Properties.Resources.turn_around;
            turn_around_btn.BackgroundImageLayout = ImageLayout.Zoom;
            turn_around_btn.Click += this.turn_around_Click;
            chess_panel.Controls.Add(turn_around_btn);
            turn_around_btn.Visible = false;
            


            cancel_btn = new Button(); //取消下棋
            //cancel_btn.AutoSize = true;
            cancel_btn.Text = "";
            cancel_btn.Width = CONTROL_BTN_WIDTH;
            cancel_btn.Height = CONTROL_BTN_WIDTH;
            cancel_btn.FlatStyle = FlatStyle.Flat;//样式
            cancel_btn.ForeColor = Color.Transparent;//前景
            cancel_btn.BackColor = Color.Transparent;//去背景
            cancel_btn.FlatAppearance.BorderSize = 0;//去边线
            cancel_btn.BackgroundImage = Properties.Resources.cancel;
            cancel_btn.BackgroundImageLayout = ImageLayout.Zoom;
            cancel_btn.Click += this.cancel_Click;
            chess_panel.Controls.Add(cancel_btn);
            cancel_btn.Visible = false;



            checked_chess_panel.Width = (int)(BLOCK_SIZE / 1.2);
            checked_chess_panel.Height = (int)(BLOCK_SIZE / 1.2);
            checked_chess_panel.BackColor = Color.FromArgb(128, 255, 255, 255);
            chess_panel.Controls.Add(checked_chess_panel);
            checked_chess_panel.Visible = false;

            DrawOperateBoard();

        }


        /// <summary>
        /// 绘制操作界面
        /// </summary>
        private void DrawOperateBoard()
        {
            operate_panel = new Panel();
            operate_panel.Width = this.Width - chess_panel.Width - 20;
            operate_panel.Height = chess_panel.Height;
            operate_panel.Location = new Point(chess_panel.Width + 2, 25);
            operate_panel.BackColor = Color.White;
            this.Controls.Add(operate_panel);

            
            DrawChessmanButton(1, true);
            DrawChessmanButton(2, false);
            alive_player_list.Add(1);
            alive_player_list.Add(2);

            if(PLAYER_NUMBER > 2)
            {
                DrawChessmanButton(3, false);
                alive_player_list.Add(3);

            }

            if (PLAYER_NUMBER > 3)
            {
                DrawChessmanButton(4, false);
                alive_player_list.Add(4);

            }

            if (PLAYER_NUMBER > 4)
            {
                DrawChessmanButton(5, false);
                alive_player_list.Add(5);

            }

            if (PLAYER_NUMBER > 5)
            {
                DrawChessmanButton(6, false);
                alive_player_list.Add(6);

            }

            label_tip.Font = new Font("楷体", 16);
            label_tip.AutoSize = true;
            label_tip.Text = "请先选择一种棋子，或点击棋盘上自己的棋子";
            label_tip.Location = new Point(20 ,310);
            operate_panel.Controls.Add(label_tip);
            label_tip.Visible = false;

            backoff_btn.Font = new Font("楷体", 14);
            //backoff.Text = "悔一步棋";
            backoff_btn.Text = "重开一局";
            backoff_btn.Location = new Point(280, 400);
            backoff_btn.Width = 120;
            backoff_btn.Height = 70;
            //backoff.Click += this.back_off_Click;
            backoff_btn.Click += this.menu_new_game_Click;
            operate_panel.Controls.Add(backoff_btn);


            label_player_number.AutoSize = true;
            label_player_number.Font = new Font("楷体", 16);
            label_player_number.Text = "剩余玩家数量: " + PLAYER_NUMBER + "名";
            label_player_number.Location = new Point(60, 428);
            operate_panel.Controls.Add(label_player_number);
            label_player_number.Focus();

            SetChessmanEnable();
        }

        #endregion


        #region 下一个玩家行动

        /// <summary>
        /// 在剩余玩家列表里寻找下一个玩家
        /// </summary>
        void PlayerNumberPlus()
        {
            active_player++;

            if (active_player > PLAYER_NUMBER)
            {
                active_player = 1;
            }

            if (!alive_player_list.Contains(active_player))//如果玩家已死亡 跳过该玩家回合
            {
                PlayerNumberPlus(); //递归调用
            }


        }


        /// <summary>
        /// 切换到下一个玩家
        /// </summary>
        void NextPlayer()
        {

            DrawChessmanButton(active_player, false);

            active_chess = null;//选中棋子取消
            checked_chess_panel.Visible = false;
            label_tip.Visible = false;

            foreach (Point key in target_block_dic.Keys)// 清空可走格子特效
            {
                chess_panel.Controls.Remove(target_block_dic[key]);
                target_block_dic[key].Dispose();
            }
            target_block_dic.Clear();

            active_chess_btn = null;

            PlayerNumberPlus();

            DrawChessmanButton(active_player, true);


            if (IsLose(active_player))
            {
                MessageBox.Show("玩家" + active_player + "被憋死，出局");
                alive_player_list.Remove(active_player);
                label_player_number.Text = "剩余玩家数量: " + alive_player_list.Count + "名";

                if (alive_player_list.Count == 1)
                {
                    MessageBox.Show("玩家" + alive_player_list[0] + " 获得胜利！");
                    chess_panel.Enabled = false;
                    return;
                }
              

                NextPlayer(); //递归调用
                


            }

        }

        #endregion


        #region 失败出局判定





        /// <summary>
        ///  判断棋子某方向是否可走
        /// </summary>
        bool GetEmptyTargerBlock(Point checked_block, int angle)
        {
            if (angle >= 360)
            {
                angle = angle - 360;
            }

            Point target_point = new Point(-1, -1);
            switch (angle)
            {
                case 0:
                    target_point = new Point(checked_block.X + 1, checked_block.Y);
                    break;
                case 45:
                    target_point = new Point(checked_block.X + 1, checked_block.Y - 1);

                    break;
                case 90:
                    target_point = new Point(checked_block.X, checked_block.Y - 1);

                    break;
                case 135:
                    target_point = new Point(checked_block.X - 1, checked_block.Y - 1);

                    break;
                case 180:
                    target_point = new Point(checked_block.X - 1, checked_block.Y);

                    break;
                case 225:
                    target_point = new Point(checked_block.X - 1, checked_block.Y + 1);

                    break;
                case 270:
                    target_point = new Point(checked_block.X, checked_block.Y + 1);

                    break;
                case 315:
                    target_point = new Point(checked_block.X + 1, checked_block.Y + 1);

                    break;
                default:
                    break;
            }

            if (!IS_BORDER_ACROSS)  // 如果棋盘边缘不允许通过
            {
                if (target_point.X < 0 || target_point.Y < 0)
                {
                    return false;
                }

                if (target_point.X >= BLOCK_NUMBER || target_point.Y >= BLOCK_NUMBER)// 如果超出最大格子
                {
                    return false;
                }
            }
            else
            {
                if (target_point.X < 0)
                {
                    target_point.X = BLOCK_NUMBER + target_point.X; // 不适用于跳棋子
                }
                if (target_point.Y < 0)
                {
                    target_point.Y = BLOCK_NUMBER + target_point.Y; // 不适用于跳棋子
                }
                if (target_point.X >= BLOCK_NUMBER)
                {
                    target_point.X = target_point.X - BLOCK_NUMBER;
                }
                if (target_point.Y >= BLOCK_NUMBER)
                {
                    target_point.Y = target_point.Y - BLOCK_NUMBER;
                }
            }
           

            if (board_map_dic.Keys.Contains(target_point)) //如果该位置存在棋子
            {
                return false;
            }

            return true;

        }


        /// <summary>
        /// 判断跳棋子是否可走
        /// </summary>
        /// <param name="checked_block"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        bool GetJumpEmptyTargerBlock(Point checked_block, int angle)
        {
            if (angle >= 360)
            {
                angle = angle - 360;
            }

            Point target_point = new Point(-1, -1);
            Point obstacle_point = new Point(-1, -1);
            switch (angle)
            {
                case 0:
                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y);
                    }
                    break;
                case 45:

                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y - 2);
                    }
                    break;
                case 90:
                    obstacle_point = new Point(checked_block.X, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X, checked_block.Y - 2);
                    }
                    break;
                case 135:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y - 2);
                    }
                    break;
                case 180:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y);
                    }
                    break;
                case 225:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y + 2);
                    }
                    break;
                case 270:
                    obstacle_point = new Point(checked_block.X, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X, checked_block.Y + 2);
                    }
                    break;
                case 315:
                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y + 2);
                    }
                    break;
                default:
                    break;
            }

            if (target_point.X < 0 || target_point.Y < 0)
            {
                return false;
            }
            if (board_map_dic.Keys.Contains(target_point)) //如果该位置存在棋子
            {
                return false;
            }
            if (target_point.X >= BLOCK_NUMBER || target_point.Y >= BLOCK_NUMBER)// 如果超出最大格子
            {
                return false;
            }

            return true;
        }


        /// <summary>
        /// 判断棋子是否可走
        /// </summary>
        /// <param name="chess"></param>
        /// <returns></returns>
        bool IsChessCanMove(BoardChess chess)
        {
            if (IS_MOVE_LIMIT) //如果有 不可连续两回合移动同一个棋子的规则
            {
                if (last_move_chess_dic.Values.Contains(chess))
                {
                    return false;
                }
            }
          

            switch (chess.ArrowType)
            {
                case ChessmanButton.ChessmanArrowType.ONE:
                    if(GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.TWO_45:
                    if(GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) || 
                       GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 45))
                    {
                        return true;
                    }else
                    {
                        return false;

                    }
                   
                case ChessmanButton.ChessmanArrowType.TWO_90:
                    if(GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) ||
                       GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 90))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.TWO_135:
                    if(GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) ||
                       GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 135))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.TWO_180:
                    if (GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection)||
                        GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 180))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.THREE_45:

                    if (GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 45) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 90))

                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.THREE_90:

                    if (GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 90) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 180))

                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.FOUR:
                    if (GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 90) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 180) ||
                    GetEmptyTargerBlock(chess.BlockLocation, chess.Deflection + 270))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ChessmanButton.ChessmanArrowType.JUMP:
                    if (GetJumpEmptyTargerBlock(chess.BlockLocation, chess.Deflection))              
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                default:
                   return  false;
            }

        }


        /// <summary>
        /// 判断当前玩家是否失败
        /// </summary>
        /// <returns></returns>
        bool IsLose(int player_num)
        {
            bool lose = true;
            bool alive = false;

            switch (player_num)
            {
                case 1:;
                    foreach(int key in red_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (red_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach(Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if(board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.RED)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                case 2:
                    foreach (int key in blue_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (blue_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach (Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if (board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.BLUE)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                case 3:
                    foreach (int key in yellow_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (yellow_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach (Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if (board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.YELLOW)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                case 4:
                    foreach (int key in green_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (green_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach (Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if (board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.GREEN)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                case 5:
                    foreach (int key in orange_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (orange_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach (Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if (board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.ORANGE)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                case 6:
                    foreach (int key in purple_chess_button_dic.Keys)  //如果有剩余棋子 判定存活
                    {
                        if (purple_chess_button_dic[key].Amount > 0)
                        {
                            return alive;
                        }
                    }

                    foreach (Point key in board_map_dic.Keys) //如果有可移动棋子 判断存活
                    {
                        if (board_map_dic[key].ArrowColor == ChessmanButton.ChessmanArrowColor.PURPLE)
                        {
                            if (IsChessCanMove(board_map_dic[key]))
                            {
                                return alive;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }


            return lose;
           
        }

        #endregion


        #region 选中棋盘中的棋子

        /// <summary>
        /// 选中棋子后 显示可以走的格子  不适用跳棋子和 2格棋子
        /// </summary> 
        void ShowAbleEmptyBlock(Point checked_block, int angle)
        {
            if (angle >= 360)
            {
                angle = angle - 360;
            }

            Point target_point = new Point(-1, -1);
            switch (angle)
            {
                case 0:
                    target_point = new Point(checked_block.X + 1, checked_block.Y);
                    break;
                case 45:
                    target_point = new Point(checked_block.X + 1, checked_block.Y - 1);

                    break;
                case 90:
                    target_point = new Point(checked_block.X, checked_block.Y - 1);

                    break;
                case 135:
                    target_point = new Point(checked_block.X - 1, checked_block.Y - 1);

                    break;
                case 180:
                    target_point = new Point(checked_block.X - 1, checked_block.Y);

                    break;
                case 225:
                    target_point = new Point(checked_block.X - 1, checked_block.Y + 1);

                    break;
                case 270:
                    target_point = new Point(checked_block.X, checked_block.Y + 1);

                    break;
                case 315:
                    target_point = new Point(checked_block.X + 1, checked_block.Y + 1);

                    break;
                default:
                    break;
            }

         

            if (!IS_BORDER_ACROSS)// 如果边缘不可通过
            {
                if (target_point.X < 0 || target_point.Y < 0)
                {
                    return;
                }

                if (target_point.X >= BLOCK_NUMBER || target_point.Y >= BLOCK_NUMBER)// 如果超出最大格子
                {
                    return;
                }

            }
            else
            {
                if(target_point.X < 0)
                {
                    target_point.X = BLOCK_NUMBER + target_point.X; // 不适用于跳棋子
                }
                if(target_point.Y < 0)
                {
                    target_point.Y = BLOCK_NUMBER + target_point.Y; // 不适用于跳棋子
                }
                if (target_point.X >= BLOCK_NUMBER)
                {
                    target_point.X = target_point.X - BLOCK_NUMBER;
                }
                if (target_point.Y >= BLOCK_NUMBER)
                {
                    target_point.Y = target_point.Y - BLOCK_NUMBER;
                }

            }

            if (board_map_dic.Keys.Contains(target_point)) //如果该位置存在棋子
            {
                return;
            }

            if (!target_block_dic.Keys.Contains(target_point))
            {
                target_block_dic.Add(target_point, new ChessBoardBlock());
                //ChessBoardBlock target_block_panel = new ChessBoardBlock(); // 为可走格子添加效果
                target_block_dic[target_point].Width = BLOCK_SIZE;
                target_block_dic[target_point].Height = BLOCK_SIZE;
                target_block_dic[target_point].BackColor = Color.FromArgb(64, 255, 0, 128);
                target_block_dic[target_point].BlockLocation = target_point;
                target_block_dic[target_point].Location = new Point(BORDER_WIDTH + BLOCK_SIZE * target_point.X, BORDER_WIDTH + BLOCK_SIZE * target_point.Y);
                target_block_dic[target_point].Click += this.target_block_Click;

                chess_panel.Controls.Add(target_block_dic[target_point]);
            }



        }


        /// <summary>
        /// 选中Jump棋子后 显示可以走的格子
        /// </summary>
        void ShowJumpAbleEmptyBlock(Point checked_block, int angle)
        {
            if (angle >= 360)
            {
                angle = angle - 360;
            }

            Point target_point = new Point(-1, -1);
            Point obstacle_point = new Point(-1, -1);
            switch (angle)
            {
                case 0:
                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y);
                    }
                    break;
                case 45:

                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y - 2);
                    }
                    break;
                case 90:
                    obstacle_point = new Point(checked_block.X, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X, checked_block.Y - 2);
                    }
                    break;
                case 135:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y - 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y - 2);
                    }
                    break;
                case 180:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y);
                    }
                    break;
                case 225:
                    obstacle_point = new Point(checked_block.X - 1, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X - 2, checked_block.Y + 2);
                    }
                    break;
                case 270:
                    obstacle_point = new Point(checked_block.X, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X, checked_block.Y + 2);
                    }
                    break;
                case 315:
                    obstacle_point = new Point(checked_block.X + 1, checked_block.Y + 1);
                    if (!board_map_dic.Keys.Contains(obstacle_point))
                    {
                        break;
                    }
                    else
                    {
                        target_point = new Point(checked_block.X + 2, checked_block.Y + 2);
                    }
                    break;
                default:
                    break;
            }

            if (target_point.X < 0 || target_point.Y < 0)
            {
                return;
            }
            if (board_map_dic.Keys.Contains(target_point)) //如果该位置存在棋子
            {
                return;
            }
            if (target_point.X >= BLOCK_NUMBER || target_point.Y >= BLOCK_NUMBER)// 如果超出最大格子
            {
                return;
            }


            if (!target_block_dic.Keys.Contains(target_point))
            {
                target_block_dic.Add(target_point, new ChessBoardBlock());
                //ChessBoardBlock target_block_panel = new ChessBoardBlock(); // 为可走格子添加效果
                target_block_dic[target_point].Width = BLOCK_SIZE;
                target_block_dic[target_point].Height = BLOCK_SIZE;
                target_block_dic[target_point].BackColor = Color.FromArgb(64, 255, 0, 128);
                target_block_dic[target_point].BlockLocation = target_point;
                target_block_dic[target_point].Location = new Point(BORDER_WIDTH + BLOCK_SIZE * target_point.X, BORDER_WIDTH + BLOCK_SIZE * target_point.Y);
                target_block_dic[target_point].Click += this.target_block_Click;

                chess_panel.Controls.Add(target_block_dic[target_point]);
            }


        }

        #endregion


        #region 棋子按钮操作

        /// <summary>
        /// 绘制每个玩家的棋子
        /// </summary>
        /// <param name="player"></param>
        private void DrawChessmanButton(int player, bool is_show)
        {


            int btn_pos_x = 16;
            int btn_pos_y = 80;
            
            switch (active_player)
            {
                case 1:
                    label_player.ForeColor = Color.Red;
                    break;
                case 2:
                    label_player.ForeColor = Color.DeepSkyBlue;
                    break;
                case 3:
                    label_player.ForeColor = Color.Khaki;
                    break;
                case 4:
                    label_player.ForeColor = Color.Green;
                    break;
                case 5:
                    label_player.ForeColor = Color.Orange;
                    break;
                case 6:
                    label_player.ForeColor = Color.MediumPurple;
                    break;
                default:
                    break;
            }
            //label_player.ForeColor = Color.Red;
            label_player.Font = new Font("楷体", 20);
            label_player.AutoSize = true;
            //label_player.Width = (int)label_player.Font.Size * 8;
            //label_player.Height = (int)label_player.Font.Size * 3 / 2;
            label_player.Text = "玩家" + active_player + ": 行动";
            label_player.Location = new Point(operate_panel.Width / 2 - (int)label_player.Font.Size * 4, btn_pos_y / 2 - (int)label_player.Font.Size / 2);
            operate_panel.Controls.Add(label_player);

           
            switch (player)
            {
                case 1:

                    if(red_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {
                       
                        foreach (int key in red_chess_button_dic.Keys)
                        {
                            red_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }


                    #region 红棋

               

                    ChessmanButton cm_r_1 = new ChessmanButton();
                    cm_r_1.Name = "r_1";
                    cm_r_1.Width = CHESS_BUTTON_SIZE;
                    cm_r_1.Height = CHESS_BUTTON_SIZE;
                    cm_r_1.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_r_1.Amount = 0;
                    red_chess_button_dic.Add(0, cm_r_1);

                    ChessmanButton cm_r_2_90 = new ChessmanButton();
                    cm_r_2_90.Name = "r_2_90";
                    cm_r_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_r_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_r_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_r_2_90.Amount = 0;
                    red_chess_button_dic.Add(1, cm_r_2_90);

                    ChessmanButton cm_r_2_180 = new ChessmanButton();
                    cm_r_2_180.Name = "r_2_180";
                    cm_r_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_r_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_r_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_r_2_180.Amount = 0;
                    red_chess_button_dic.Add(2, cm_r_2_180);

                    ChessmanButton cm_r_2_45 = new ChessmanButton();
                    cm_r_2_45.Name = "r_2_45";
                    cm_r_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_r_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_r_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_r_2_45.Amount = 0;
                    red_chess_button_dic.Add(3, cm_r_2_45);

                    
                    ChessmanButton cm_r_2_135 = new ChessmanButton();
                    cm_r_2_135.Name = "r_2_135";
                    cm_r_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_r_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_r_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_r_2_135.Amount = 0;
                    red_chess_button_dic.Add(4, cm_r_2_135);

                    
                    ChessmanButton cm_r_3_90 = new ChessmanButton();
                    cm_r_3_90.Name = "r_3_90";
                    cm_r_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_r_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_r_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_r_3_90.Amount = 0;
                    red_chess_button_dic.Add(5, cm_r_3_90);


                    ChessmanButton cm_r_3_45 = new ChessmanButton();
                    cm_r_3_45.Name = "r_3_45";
                    cm_r_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_r_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_r_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_r_3_45.Amount = 0;
                    red_chess_button_dic.Add(6, cm_r_3_45);
                  

                    ChessmanButton cm_r_4 = new ChessmanButton();
                    cm_r_4.Name = "r_4";
                    cm_r_4.Width = CHESS_BUTTON_SIZE;
                    cm_r_4.Height = CHESS_BUTTON_SIZE;
                    cm_r_4.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_r_4.Amount = 0;
                    red_chess_button_dic.Add(7, cm_r_4);

                    ChessmanButton cm_r_jump = new ChessmanButton();
                    cm_r_jump.Name = "r_jump";
                    cm_r_jump.Width = CHESS_BUTTON_SIZE;
                    cm_r_jump.Height = CHESS_BUTTON_SIZE;
                    cm_r_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    cm_r_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_r_jump.Amount = 0;
                    red_chess_button_dic.Add(8, cm_r_jump);


                    for (int i = 0; i < red_chess_button_dic.Count; i++)
                    {
                        if(i < 6)
                        {
                            red_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(red_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            red_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(red_chess_button_dic[i]);
                        }
                    }

                    #endregion

                    if (!is_show)
                    {
                        foreach(int key in red_chess_button_dic.Keys)
                        {
                            red_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                case 2:

                    if (blue_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {

                        foreach (int key in blue_chess_button_dic.Keys)
                        {
                            blue_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }

                    #region 蓝棋

                    //Label player2 = new Label();
                    //player2.ForeColor = Color.DeepSkyBlue;
                    //player2.Font = new Font("楷体", 16);
                    //player2.Width = (int)player2.Font.Size * 8;
                    //player2.Height = (int)player2.Font.Size * 3 / 2;
                    //player2.Text = "玩家2:行动";
                    //player2.Location = new Point(operate_panel.Width / 2 - (int)player2.Font.Size * 3, btn_pos_y / 2 - (int)player2.Font.Size / 2);
                    //operate_panel.Controls.Add(player2);


                    ChessmanButton cm_b_1 = new ChessmanButton();
                    cm_b_1.Name = "b_1";
                    cm_b_1.Width = CHESS_BUTTON_SIZE;
                    cm_b_1.Height = CHESS_BUTTON_SIZE;
                    cm_b_1.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_b_1.Amount = 0;
                    blue_chess_button_dic.Add(0, cm_b_1);

                    ChessmanButton cm_b_2_90 = new ChessmanButton();
                    cm_b_2_90.Name = "b_2_90";
                    cm_b_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_b_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_b_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_b_2_90.Amount = 0;
                    blue_chess_button_dic.Add(1, cm_b_2_90);


                    ChessmanButton cm_b_2_180 = new ChessmanButton();
                    cm_b_2_180.Name = "b_2_180";
                    cm_b_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_b_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_b_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_b_2_180.Amount = 0;
                    blue_chess_button_dic.Add(2, cm_b_2_180);


                    ChessmanButton cm_b_2_45 = new ChessmanButton();
                    cm_b_2_45.Name = "b_2_45";
                    cm_b_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_b_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_b_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_b_2_45.Amount = 0;
                    blue_chess_button_dic.Add(3, cm_b_2_45);

                    
                    ChessmanButton cm_b_2_135 = new ChessmanButton();
                    cm_b_2_135.Name = "b_2_135";
                    cm_b_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_b_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_b_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_b_2_135.Amount = 0;
                    blue_chess_button_dic.Add(4, cm_b_2_135);


                    ChessmanButton cm_b_3_90 = new ChessmanButton();
                    cm_b_3_90.Name = "b_3_90";
                    cm_b_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_b_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_b_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_b_3_90.Amount = 0;
                    blue_chess_button_dic.Add(5, cm_b_3_90);

                    ChessmanButton cm_b_3_45 = new ChessmanButton();
                    cm_b_3_45.Name = "b_3_45";
                    cm_b_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_b_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_b_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_b_3_45.Amount = 0;
                    blue_chess_button_dic.Add(6, cm_b_3_45);

                    ChessmanButton cm_b_4 = new ChessmanButton();
                    cm_b_4.Name = "b_4";
                    cm_b_4.Width = CHESS_BUTTON_SIZE;
                    cm_b_4.Height = CHESS_BUTTON_SIZE;
                    cm_b_4.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_b_4.Amount = 0;
                    blue_chess_button_dic.Add(7, cm_b_4);

                    ChessmanButton cm_b_jump = new ChessmanButton();
                    cm_b_jump.Name = "b_jump";
                    cm_b_jump.Width = CHESS_BUTTON_SIZE;
                    cm_b_jump.Height = CHESS_BUTTON_SIZE;
                    cm_b_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    cm_b_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_b_jump.Amount = 0;
                    blue_chess_button_dic.Add(8, cm_b_jump);

                    for (int i = 0; i < blue_chess_button_dic.Count; i++)
                    {
                        if (i < 6)
                        {
                            blue_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(blue_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            blue_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(blue_chess_button_dic[i]);
                        }
                    }

                    #endregion

                    if (!is_show)
                    {
                        foreach (int key in blue_chess_button_dic.Keys)
                        {
                            blue_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                case 3:

                    if (yellow_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {

                        foreach (int key in yellow_chess_button_dic.Keys)
                        {
                            yellow_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }

                    #region 黄棋

                    //Label player3 = new Label();
                    //player3.ForeColor = Color.Yellow;
                    //player3.Font = new Font("楷体", 16);
                    //player3.Width = (int)player3.Font.Size * 8;
                    //player3.Height = (int)player3.Font.Size * 3 / 2;
                    //player3.Text = "玩家3:行动";
                    //player3.Location = new Point(operate_panel.Width / 2 - (int)player3.Font.Size * 3, btn_pos_y / 2 - (int)player3.Font.Size / 2);
                    //operate_panel.Controls.Add(player3);

                    ChessmanButton cm_y_1 = new ChessmanButton();
                    cm_y_1.Name = "y_1";
                    cm_y_1.Width = CHESS_BUTTON_SIZE;
                    cm_y_1.Height = CHESS_BUTTON_SIZE;
                    cm_y_1.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_y_1.Amount = 0;
                    yellow_chess_button_dic.Add(0, cm_y_1);

                    ChessmanButton cm_y_2_90 = new ChessmanButton();
                    cm_y_2_90.Name = "y_2_90";
                    cm_y_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_y_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_y_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_y_2_90.Amount = 0;
                    yellow_chess_button_dic.Add(1, cm_y_2_90);

                    ChessmanButton cm_y_2_180 = new ChessmanButton();
                    cm_y_2_180.Name = "y_2_180";
                    cm_y_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_y_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_y_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_y_2_180.Amount = 0;
                    yellow_chess_button_dic.Add(2, cm_y_2_180);

                    ChessmanButton cm_y_2_45 = new ChessmanButton();
                    cm_y_2_45.Name = "y_2_45";
                    cm_y_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_y_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_y_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_y_2_45.Amount = 0;
                    yellow_chess_button_dic.Add(3, cm_y_2_45);

                       
                    ChessmanButton cm_y_2_135 = new ChessmanButton();
                    cm_y_2_135.Name = "y_2_135";
                    cm_y_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_y_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_y_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_y_2_135.Amount = 0;
                    yellow_chess_button_dic.Add(4, cm_y_2_135);


                    ChessmanButton cm_y_3_90 = new ChessmanButton();
                    cm_y_3_90.Name = "y_3_90";
                    cm_y_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_y_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_y_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_y_3_90.Amount = 0;
                    yellow_chess_button_dic.Add(5, cm_y_3_90);

                    ChessmanButton cm_y_3_45 = new ChessmanButton();
                    cm_y_3_45.Name = "y_3_45";
                    cm_y_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_y_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_y_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_y_3_45.Amount = 0;
                    yellow_chess_button_dic.Add(6, cm_y_3_45);

                    ChessmanButton cm_y_4 = new ChessmanButton();
                    cm_y_4.Name = "y_4";
                    cm_y_4.Width = CHESS_BUTTON_SIZE;
                    cm_y_4.Height = CHESS_BUTTON_SIZE;
                    cm_y_4.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_y_4.Amount = 0;
                    yellow_chess_button_dic.Add(7, cm_y_4);

                    ChessmanButton cm_y_jump = new ChessmanButton();
                    cm_y_jump.Name = "y_jump";
                    cm_y_jump.Width = CHESS_BUTTON_SIZE;
                    cm_y_jump.Height = CHESS_BUTTON_SIZE;
                    cm_y_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    cm_y_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_y_jump.Amount = 0;
                    yellow_chess_button_dic.Add(8, cm_y_jump);



                    for (int i = 0; i < yellow_chess_button_dic.Count; i++)
                    {
                        if (i < 6)
                        {
                            yellow_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(yellow_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            yellow_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(yellow_chess_button_dic[i]);
                        }
                    }


                    #endregion

                    if (!is_show)
                    {
                        foreach (int key in yellow_chess_button_dic.Keys)
                        {
                            yellow_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                case 4:

                    if (green_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {

                        foreach (int key in green_chess_button_dic.Keys)
                        {
                            green_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }

                    #region 绿棋

                    //Label player4 = new Label();
                    //player4.ForeColor = Color.Green;
                    //player4.Font = new Font("楷体", 16);
                    //player4.Width = (int)player4.Font.Size * 8;
                    //player4.Height = (int)player4.Font.Size * 3 / 2;
                    //player4.Text = "玩家4:行动";
                    //player4.Location = new Point(operate_panel.Width / 2 - (int)player4.Font.Size * 3, btn_pos_y / 2 - (int)player4.Font.Size / 2);
                    //operate_panel.Controls.Add(player4);

                    ChessmanButton cm_g_1 = new ChessmanButton();
                    cm_g_1.Name = "g_1";
                    cm_g_1.Width = CHESS_BUTTON_SIZE;
                    cm_g_1.Height = CHESS_BUTTON_SIZE;
                    cm_g_1.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_g_1.Amount = 0;
                    green_chess_button_dic.Add(0, cm_g_1);

                    ChessmanButton cm_g_2_90 = new ChessmanButton();
                    cm_g_2_90.Name = "g_2_90";
                    cm_g_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_g_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_g_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_g_2_90.Amount = 0;
                    green_chess_button_dic.Add(1, cm_g_2_90);

                    ChessmanButton cm_g_2_180 = new ChessmanButton();
                    cm_g_2_180.Name = "g_2_180";
                    cm_g_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_g_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_g_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_g_2_180.Amount = 0;
                    green_chess_button_dic.Add(2, cm_g_2_180);

                    ChessmanButton cm_g_2_45 = new ChessmanButton();
                    cm_g_2_45.Name = "g_2_45";
                    cm_g_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_g_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_g_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_g_2_45.Amount = 0;
                    green_chess_button_dic.Add(3, cm_g_2_45);

                      
                    ChessmanButton cm_g_2_135 = new ChessmanButton();
                    cm_g_2_135.Name = "g_2_135";
                    cm_g_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_g_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_g_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_g_2_135.Amount = 0;
                    green_chess_button_dic.Add(4, cm_g_2_135);

                     
                    ChessmanButton cm_g_3_90 = new ChessmanButton();
                    cm_g_3_90.Name = "g_3_90";
                    cm_g_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_g_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_g_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_g_3_90.Amount = 0;
                    green_chess_button_dic.Add(5, cm_g_3_90);

                    ChessmanButton cm_g_3_45 = new ChessmanButton();
                    cm_g_3_45.Name  = "g_3_45";
                    cm_g_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_g_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_g_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_g_3_45.Amount = 0;
                    green_chess_button_dic.Add(6, cm_g_3_45);

                    ChessmanButton cm_g_4 = new ChessmanButton();
                    cm_g_4.Name = "g_4";
                    cm_g_4.Width = CHESS_BUTTON_SIZE;
                    cm_g_4.Height = CHESS_BUTTON_SIZE;
                    cm_g_4.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_g_4.Amount = 0;
                    green_chess_button_dic.Add(7, cm_g_4);

                    ChessmanButton cm_g_jump = new ChessmanButton();
                    cm_g_jump.Name = "g_jump";
                    cm_g_jump.Width = CHESS_BUTTON_SIZE;
                    cm_g_jump.Height = CHESS_BUTTON_SIZE;
                    cm_g_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    cm_g_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_g_jump.Amount = 0;
                    green_chess_button_dic.Add(8, cm_g_jump);



                    for (int i = 0; i < green_chess_button_dic.Count; i++)
                    {
                        if (i < 6)
                        {
                            green_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(green_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            green_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(green_chess_button_dic[i]);
                        }
                    }

                    

                    #endregion

                    if (!is_show)
                    {
                        foreach (int key in green_chess_button_dic.Keys)
                        {
                            green_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                case 5:

                    if (orange_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {

                        foreach (int key in orange_chess_button_dic.Keys)
                        {
                            orange_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }

                    #region 橙棋

                    //Label player5 = new Label();
                    //player5.ForeColor = Color.Orange;
                    //player5.Font = new Font("楷体", 16);
                    //player5.Width = (int)player5.Font.Size * 8;
                    //player5.Height = (int)player5.Font.Size * 3 / 2;
                    //player5.Text = "玩家5:行动";
                    //player5.Location = new Point(operate_panel.Width / 2 - (int)player5.Font.Size * 3, btn_pos_y / 2 - (int)player5.Font.Size / 2);
                    //operate_panel.Controls.Add(player5);


                    ChessmanButton cm_o_1 = new ChessmanButton();
                    cm_o_1.Name = "o_1";
                    cm_o_1.Width = CHESS_BUTTON_SIZE;
                    cm_o_1.Height = CHESS_BUTTON_SIZE;
                    cm_o_1.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_o_1.Amount = 0;
                    orange_chess_button_dic.Add(0, cm_o_1);


                    ChessmanButton cm_o_2_90 = new ChessmanButton();
                    cm_o_2_90.Name = "o_2_90";
                    cm_o_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_o_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_o_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_o_2_90.Amount = 0;
                    orange_chess_button_dic.Add(1, cm_o_2_90);

                    ChessmanButton cm_o_2_180 = new ChessmanButton();
                    cm_o_2_180.Name = "o_2_180";
                    cm_o_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_o_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_o_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_o_2_180.Amount = 0;
                    orange_chess_button_dic.Add(2, cm_o_2_180);


                    ChessmanButton cm_o_2_45 = new ChessmanButton();
                    cm_o_2_45.Name = "o_2_45";
                    cm_o_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_o_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_o_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_o_2_45.Amount = 0;
                    orange_chess_button_dic.Add(3, cm_o_2_45);

                    ChessmanButton cm_o_2_135 = new ChessmanButton();
                    cm_o_2_135.Name = "o_2_135";
                    cm_o_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_o_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_o_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_o_2_135.Amount = 0;
                    orange_chess_button_dic.Add(4, cm_o_2_135);

                 
                    ChessmanButton cm_o_3_90 = new ChessmanButton();
                    cm_o_3_90.Name = "o_3_90";
                    cm_o_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_o_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_o_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_o_3_90.Amount = 0;
                    orange_chess_button_dic.Add(5, cm_o_3_90);

                    ChessmanButton cm_o_3_45 = new ChessmanButton();
                    cm_o_3_45.Name = "o_3_45";
                    cm_o_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_o_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_o_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_o_3_45.Amount = 0;
                    orange_chess_button_dic.Add(6, cm_o_3_45);

                    ChessmanButton cm_o_4 = new ChessmanButton();
                    cm_o_4.Name = "o_4";
                    cm_o_4.Width = CHESS_BUTTON_SIZE;
                    cm_o_4.Height = CHESS_BUTTON_SIZE;
                    cm_o_4.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_o_4.Amount = 0;
                    orange_chess_button_dic.Add(7, cm_o_4);

                    ChessmanButton cm_o_jump = new ChessmanButton();
                    cm_o_jump.Name = "o_jump";
                    cm_o_jump.Width = CHESS_BUTTON_SIZE;
                    cm_o_jump.Height = CHESS_BUTTON_SIZE;
                    cm_o_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    cm_o_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_o_jump.Amount = 0;
                    orange_chess_button_dic.Add(8, cm_o_jump);


                    for (int i = 0; i < orange_chess_button_dic.Count; i++)
                    {
                        if (i < 6)
                        {
                            orange_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(orange_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            orange_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(orange_chess_button_dic[i]);
                        }
                    }
                    

                    #endregion

                    if (!is_show)
                    {
                        foreach (int key in orange_chess_button_dic.Keys)
                        {
                            orange_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                case 6:
                    if (purple_chess_button_dic.Count > 0) //如果字典中已经有值 直接跳过绘制按钮，执行显示/隐藏功能
                    {

                        foreach (int key in purple_chess_button_dic.Keys)
                        {
                            purple_chess_button_dic[key].Visible = is_show;
                        }
                        return;

                    }

                    #region 紫棋

                    //Label player6 = new Label();
                    //player6.ForeColor = Color.MediumPurple;
                    //player6.Font = new Font("楷体", 16);
                    //player6.Width = (int)player6.Font.Size * 8;
                    //player6.Height = (int)player6.Font.Size * 3 / 2;
                    //player6.Text = "玩家6:行动";
                    //player6.Location = new Point(operate_panel.Width / 2 - (int)player6.Font.Size * 3, btn_pos_y / 2 - (int)player6.Font.Size / 2);
                    //operate_panel.Controls.Add(player6);

                    ChessmanButton cm_p_1 = new ChessmanButton();
                    cm_p_1.Name = "p_1";
                    cm_p_1.Width = CHESS_BUTTON_SIZE;
                    cm_p_1.Height = CHESS_BUTTON_SIZE;
                    cm_p_1.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_1.ArrowType = ChessmanButton.ChessmanArrowType.ONE;
                    cm_p_1.Amount = 0;
                    purple_chess_button_dic.Add(0, cm_p_1);


                    ChessmanButton cm_p_2_90 = new ChessmanButton();
                    cm_p_2_90.Name = "p_2_90";
                    cm_p_2_90.Width = CHESS_BUTTON_SIZE;
                    cm_p_2_90.Height = CHESS_BUTTON_SIZE;
                    cm_p_2_90.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_2_90.ArrowType = ChessmanButton.ChessmanArrowType.TWO_90;
                    cm_p_2_90.Amount = 0;
                    purple_chess_button_dic.Add(1, cm_p_2_90);

                    ChessmanButton cm_p_2_180 = new ChessmanButton();
                    cm_p_2_180.Name = "p_2_180";
                    cm_p_2_180.Width = CHESS_BUTTON_SIZE;
                    cm_p_2_180.Height = CHESS_BUTTON_SIZE;
                    cm_p_2_180.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_2_180.ArrowType = ChessmanButton.ChessmanArrowType.TWO_180;
                    cm_p_2_180.Amount = 0;
                    purple_chess_button_dic.Add(2, cm_p_2_180);

                    ChessmanButton cm_p_2_45 = new ChessmanButton();
                    cm_p_2_45.Name = "p_2_45";
                    cm_p_2_45.Width = CHESS_BUTTON_SIZE;
                    cm_p_2_45.Height = CHESS_BUTTON_SIZE;
                    cm_p_2_45.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_2_45.ArrowType = ChessmanButton.ChessmanArrowType.TWO_45;
                    cm_p_2_45.Amount = 0;
                    purple_chess_button_dic.Add(3, cm_p_2_45);


                    ChessmanButton cm_p_2_135 = new ChessmanButton();
                    cm_p_2_135.Name = "p_2_135";
                    cm_p_2_135.Width = CHESS_BUTTON_SIZE;
                    cm_p_2_135.Height = CHESS_BUTTON_SIZE;
                    cm_p_2_135.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_2_135.ArrowType = ChessmanButton.ChessmanArrowType.TWO_135;
                    cm_p_2_135.Amount = 0;
                    purple_chess_button_dic.Add(4, cm_p_2_135);

                        
                    ChessmanButton cm_p_3_90 = new ChessmanButton();
                    cm_p_3_90.Name = "p_3_90";
                    cm_p_3_90.Width = CHESS_BUTTON_SIZE;
                    cm_p_3_90.Height = CHESS_BUTTON_SIZE;
                    cm_p_3_90.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_3_90.ArrowType = ChessmanButton.ChessmanArrowType.THREE_90;
                    cm_p_3_90.Amount = 0;
                    purple_chess_button_dic.Add(5, cm_p_3_90);

                    ChessmanButton cm_p_3_45 = new ChessmanButton();
                    cm_p_3_45.Name = "p_3_45";
                    cm_p_3_45.Width = CHESS_BUTTON_SIZE;
                    cm_p_3_45.Height = CHESS_BUTTON_SIZE;
                    cm_p_3_45.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_3_45.ArrowType = ChessmanButton.ChessmanArrowType.THREE_45;
                    cm_p_3_45.Amount = 0;
                    purple_chess_button_dic.Add(6, cm_p_3_45);

                    ChessmanButton cm_p_4 = new ChessmanButton();
                    cm_p_4.Name = "p_4";
                    cm_p_4.Width = CHESS_BUTTON_SIZE;
                    cm_p_4.Height = CHESS_BUTTON_SIZE;
                    cm_p_4.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_4.ArrowType = ChessmanButton.ChessmanArrowType.FOUR;
                    cm_p_4.Amount = 0;
                    purple_chess_button_dic.Add(7, cm_p_4);

                    ChessmanButton cm_p_jump = new ChessmanButton();
                    cm_p_jump.Name = "p_jump";
                    cm_p_jump.Width = CHESS_BUTTON_SIZE;
                    cm_p_jump.Height = CHESS_BUTTON_SIZE;
                    cm_p_jump.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    cm_p_jump.ArrowType = ChessmanButton.ChessmanArrowType.JUMP;
                    cm_p_jump.Amount = 0;
                    purple_chess_button_dic.Add(8, cm_p_jump);



                    for (int i = 0; i < purple_chess_button_dic.Count; i++)
                    {
                        if (i < 6)
                        {
                            purple_chess_button_dic[i].Location = new Point(btn_pos_x + i * CHESS_BUTTON_SIZE, btn_pos_y);
                            operate_panel.Controls.Add(purple_chess_button_dic[i]);
                        }
                        else if (i >= 6 && i < 12)
                        {
                            purple_chess_button_dic[i].Location = new Point(btn_pos_x + (i - 6) * CHESS_BUTTON_SIZE, btn_pos_y + CHESS_BUTTON_SIZE);
                            operate_panel.Controls.Add(purple_chess_button_dic[i]);
                        }
                    }

                    

                    #endregion

                    if (!is_show)
                    {
                        foreach (int key in purple_chess_button_dic.Keys)
                        {
                            purple_chess_button_dic[key].Visible = false;
                        }
                    }

                    break;
                default:
                    break;
            }
         
        }


        /// <summary>
        /// 棋子按钮数量增加
        /// </summary>
        private void ChessmanAmountPlus(int player_amount,int index)
        {
           // MessageBox.Show(player_amount + "   " + index);
            red_chess_button_dic[index].Amount ++;
            blue_chess_button_dic[index].Amount ++;

          
            if (player_amount > 2)
            {
                yellow_chess_button_dic[index].Amount++;
            }

            if (player_amount > 3)
            {
                green_chess_button_dic[index].Amount++;
            }

            if (player_amount > 4)
            {
                orange_chess_button_dic[index].Amount++;
            }

            if (player_amount > 5)
            {
                purple_chess_button_dic[index].Amount++;
            }

        }


        /// <summary>
        /// 设置棋子按钮数量
        /// </summary>
        private void SetChessmanEnable()
        {
            ChessmanAmountPlus(PLAYER_NUMBER,0);   // 1  1
            ChessmanAmountPlus(PLAYER_NUMBER, 1);  


            if (CHESSMAN_AMOUNT > 2)  // 3棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 2);   //1 1 1
            }

            if (CHESSMAN_AMOUNT > 3) // 4棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 3);   //1 1 1 1
            }


            if (CHESSMAN_AMOUNT > 4) // 5棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 4);   //1 1 1 1 1
            }


            if (CHESSMAN_AMOUNT > 5) //6棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 5);   //1 1 1 1 1 1
            }

            if (CHESSMAN_AMOUNT > 6) //7棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 8);   //1 1 1 1 1 1 0 0 1
            }


            if (CHESSMAN_AMOUNT > 7) //8 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 6);   //1 1 1 1 1 1 1 0 1

            }

            if (CHESSMAN_AMOUNT > 8) //9 棋子 
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 0);   //2 1 1 1 1 1 1 0 1

            }

            if (CHESSMAN_AMOUNT > 9) //10 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 7);   //2 1 1 1 1 1 1 1 1

            }

            if (CHESSMAN_AMOUNT > 10) //11 棋子   
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 1);   //2 2 1 1 1 1 1 1 1

            }

            if (CHESSMAN_AMOUNT > 13) //14 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 2);   //2 2 2 2 2 1 1 1 1
                ChessmanAmountPlus(PLAYER_NUMBER, 3);
                ChessmanAmountPlus(PLAYER_NUMBER, 4);
            }

            if (CHESSMAN_AMOUNT > 14) //15 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 5);   //2 2 2 2 2 2 1 1 1

            }

            if (CHESSMAN_AMOUNT > 17) //18 棋子
            {
                   
                ChessmanAmountPlus(PLAYER_NUMBER, 0); //3 3 3 2 2 2 1 1 1
                ChessmanAmountPlus(PLAYER_NUMBER, 1);
                ChessmanAmountPlus(PLAYER_NUMBER, 2);

            }

            if (CHESSMAN_AMOUNT > 20) //21 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 3);   //3 3 3 3 3 2 2 1 1
                ChessmanAmountPlus(PLAYER_NUMBER, 4);
                ChessmanAmountPlus(PLAYER_NUMBER, 6);
            }

            if (CHESSMAN_AMOUNT > 27) //28 棋子
            {
                ChessmanAmountPlus(PLAYER_NUMBER, 0);   //5 4 4 4 4  2 2 1 2
                ChessmanAmountPlus(PLAYER_NUMBER, 0);
                ChessmanAmountPlus(PLAYER_NUMBER, 1);
                ChessmanAmountPlus(PLAYER_NUMBER, 2);
                ChessmanAmountPlus(PLAYER_NUMBER, 3);
                ChessmanAmountPlus(PLAYER_NUMBER, 4);
                ChessmanAmountPlus(PLAYER_NUMBER, 8);
            }

            /*****************对照表*****************/
            /* 每行格子数量    玩家人数    棋子数   */
            /*     4               2         6      */
            /*     4               3         4      */
            /*     4               4         3      */
            /*     4               5         2      */
            /*     4               6         2      */
            /*     5               2         10     */
            /*     5               3         7      */
            /*     5               4         5      */
            /*     5               5         4      */
            /*     5               6         3      */
            /*     6               2         15     */
            /*     6               3         10     */
            /*     6               4         8      */
            /*     6               5         6      */
            /*     6               6         5      */
            /*     7               2         21     */
            /*     7               3         14     */
            /*     7               4         11     */
            /*     7               5         8      */
            /*     7               6         7      */
            /*     8               2         28     */
            /*     8               3         19     */
            /*     8               4         14     */
            /*     8               5         11     */
            /*     8               6         9      */
            /****************************************/

        }

        #endregion


        #region 控件事件

        /// <summary>
        /// 棋盘渲染事件 绘制棋盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chess_panel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
            Pen p = new Pen(Color.Black, 1);//定义了一个蓝色,宽度为的画笔
            SolidBrush sb = new SolidBrush(Color.Black);
            Font font = new Font("微软雅黑", 20);

            for (int i = 0; i <= BLOCK_NUMBER; i++)
            {
                g.DrawLine(p, new Point(BORDER_WIDTH, i * BLOCK_SIZE + BORDER_WIDTH), new Point( BLOCK_NUMBER * BLOCK_SIZE + BORDER_WIDTH, i * BLOCK_SIZE + BORDER_WIDTH));//画横线
                g.DrawLine(p, new Point(BORDER_WIDTH + i * BLOCK_SIZE, BORDER_WIDTH), new Point( i * BLOCK_SIZE + BORDER_WIDTH, BLOCK_NUMBER * BLOCK_SIZE + BORDER_WIDTH));//画竖线
            }
            for (int i = 0; i < BLOCK_NUMBER; i++)
            {
                g.DrawString((i + 1).ToString(), font, sb, new Point(10, i * BLOCK_SIZE + BLOCK_SIZE / 2 + BORDER_WIDTH - (int)font.Size));
                g.DrawString(Convert.ToChar(i + 65).ToString(), font, sb, new Point(i * BLOCK_SIZE + BLOCK_SIZE / 2 + BORDER_WIDTH - (int)(font.Size/2), 0));//Convert.ToChar()用于将ASCII转为字符
            }

        }


        /// <summary>
        /// 棋盘点击事件 下棋
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chess_panel_MouseClick(object sender, MouseEventArgs e)
        {
            Point location = e.Location; //点击到棋盘外部 返回

            if( location.X <= BORDER_WIDTH || 
                location.Y <= BORDER_WIDTH || 
                location.X >= BORDER_WIDTH + BLOCK_NUMBER * BLOCK_SIZE || 
                location.Y >= BORDER_WIDTH + BLOCK_NUMBER * BLOCK_SIZE)
            {
                return;
            }
             

            if(active_chess_btn == null) // 没有选中的棋子按钮 返回
            {
                label_tip.Visible = true;//显示提示
                return;
            }

            label_tip.Visible = false;

            
            Point chess_block = new Point((location.X - BORDER_WIDTH) / BLOCK_SIZE, (location.Y - BORDER_WIDTH) / BLOCK_SIZE);// 棋子所在格子

            if (board_map_dic.Keys.Contains(chess_block)) //如果棋盘上已有棋子
            { 
                return;
            }
           
            temp_chess.Location = new Point(BORDER_WIDTH + BLOCK_SIZE * chess_block.X + BLOCK_SIZE / 12,
                                            BORDER_WIDTH + BLOCK_SIZE * chess_block.Y + BLOCK_SIZE / 12);

            int btn_group_x = BORDER_WIDTH + BLOCK_SIZE * (chess_block.X + 1);//按钮组显示在棋子右侧
            int btn_group_y = BORDER_WIDTH + BLOCK_SIZE * chess_block.Y + BLOCK_SIZE / 2 - 90;//按钮组显示在棋子右侧
            temp_chess.BlockLocation = chess_block;

            if (chess_panel.Width -  btn_group_x < CONTROL_BTN_WIDTH)
            {
                btn_group_x = btn_group_x - BLOCK_SIZE - CONTROL_BTN_WIDTH;
            }
            confirm_btn.Location = new Point(btn_group_x, btn_group_y);// turn_around_btn.Height);
            turn_around_btn.Location = new Point(btn_group_x, btn_group_y + confirm_btn.Height);
            cancel_btn.Location = new Point(btn_group_x, btn_group_y + confirm_btn.Height + turn_around_btn.Height);// turn_around_btn.Height + confirm_btn.Height);
            

            temp_chess.Visible = true;

            confirm_btn.Visible = true;
            turn_around_btn.Visible = true;
            cancel_btn.Visible = true;

           // MessageBox.Show(active_chess_btn.ArrowType.ToString());

            switch (active_chess_btn.Name.Substring(0, 1))
            {
                case "r":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.RED;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;

                    break;
                case "b":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.BLUE;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;

                    break;
                case "y":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.YELLOW;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;

                    break;
                case "g":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.GREEN;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;

                    break;
                case "o":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.ORANGE;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;
                    break;
                case "p":
                    temp_chess.ArrowColor = ChessmanButton.ChessmanArrowColor.PURPLE;
                    temp_chess.ArrowType = active_chess_btn.ArrowType;

                    break;
                default:
                    break;

            }

            temp_chess.Deflection = 0;
       
        }


        /// <summary>
        /// 悔棋按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void back_off_Click(object sender, EventArgs e)
        {
          
        }


        /// <summary>
        /// 旋转按钮 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void turn_around_Click(object sender, EventArgs e)
        {
            temp_chess.Deflection += 45;
            if(temp_chess.Deflection >= 360)
            {
                temp_chess.Deflection = temp_chess.Deflection - 360;
            }
        }


        /// <summary>
        /// 确认按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confirm_Click(object sender, EventArgs e)
        {
            temp_chess.Visible = false;

            confirm_btn.Visible = false;
            turn_around_btn.Visible = false;
            cancel_btn.Visible = false;


            board_map_dic.Add(temp_chess.BlockLocation, new BoardChess());
            board_map_dic[temp_chess.BlockLocation].Width = temp_chess.Width;
            board_map_dic[temp_chess.BlockLocation].Height = temp_chess.Width;
            board_map_dic[temp_chess.BlockLocation].Location = temp_chess.Location;

            board_map_dic[temp_chess.BlockLocation].ArrowColor = temp_chess.ArrowColor;

            board_map_dic[temp_chess.BlockLocation].ArrowType = temp_chess.ArrowType;
            board_map_dic[temp_chess.BlockLocation].Deflection = temp_chess.Deflection;

            board_map_dic[temp_chess.BlockLocation].BlockLocation = temp_chess.BlockLocation;
            board_map_dic[temp_chess.BlockLocation].Click += board_chess_Click;
            

            chess_panel.Controls.Add(board_map_dic[temp_chess.BlockLocation]);
           
            // MessageBox.Show(active_chess_btn.ArrowType.ToString());
            active_chess_btn.Amount--;

            if (IS_MOVE_LIMIT) //如果有 不可连续两回合移动同一个棋子的规则
            {
                if (last_move_chess_dic.Keys.Contains(active_player))
                {
                    last_move_chess_dic[active_player].IsAbleMove = true;
                    last_move_chess_dic.Remove(active_player);  // 上一回合移动的棋子解除限制
                }
            }
            
            
            NextPlayer();
        }


        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancel_Click(object sender, EventArgs e)
        {
            temp_chess.Visible = false;

            confirm_btn.Visible = false;
            turn_around_btn.Visible = false;
            cancel_btn.Visible = false;
        }


        /// <summary>
        /// 棋子点击事件 准备移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void board_chess_Click(object sender, EventArgs e)
        {
            if (target_block_dic.Count > 0)// 移除上一次显示高亮的格子
            {
                foreach (Point key in target_block_dic.Keys)
                {
                    chess_panel.Controls.Remove(target_block_dic[key]);
                    target_block_dic[key].Dispose();
                }
                target_block_dic.Clear();
            }


            BoardChess chess = (BoardChess)sender;

         

            switch (chess.ArrowColor) // 判断点击的是不是自己的棋子
            {
                case ChessmanButton.ChessmanArrowColor.RED:
                    if(active_player != 1)
                    {
                        return;
                    }
                    break;
                case ChessmanButton.ChessmanArrowColor.BLUE:
                    if (active_player != 2)
                    {
                        return;
                    }
                    break;
                
                case ChessmanButton.ChessmanArrowColor.YELLOW:
                    if (active_player != 3)
                    {
                        return;
                    }
                    break;
                case ChessmanButton.ChessmanArrowColor.GREEN:
                    if (active_player != 4)
                    {
                        return;
                    }
                    break;
                case ChessmanButton.ChessmanArrowColor.ORANGE:
                    if (active_player != 5)
                    {
                        return;
                    }
                    break;
                case ChessmanButton.ChessmanArrowColor.PURPLE:
                    if (active_player != 6)
                    {
                        return;
                    }
                    break;
                default:
                    return;
            }

            checked_chess_panel.BlockLocation = chess.BlockLocation; // 为选中棋子添加效果
            checked_chess_panel.Location = new Point(BORDER_WIDTH + BLOCK_SIZE * chess.BlockLocation.X + BLOCK_SIZE / 12, BORDER_WIDTH + BLOCK_SIZE * chess.BlockLocation.Y + BLOCK_SIZE / 12);
            
            if (IS_MOVE_LIMIT)// 如果有 不可连续两回合移动同一个棋子的规则
            {
                if (last_move_chess_dic.Keys.Contains(active_player))
                {
                
                    if (last_move_chess_dic[active_player] == chess) //是上一回合移动的，不可移动
                    {
                        checked_chess_panel.BackColor = Color.FromArgb(128, 64, 64, 64);
                        checked_chess_panel.Visible = true;
                        checked_chess_panel.SendToBack();
                        return;
                    }
               
                }
            }

            checked_chess_panel.BackColor = Color.FromArgb(128, 255, 255, 255);
            checked_chess_panel.Visible = true;
            checked_chess_panel.SendToBack();

            active_chess = chess;

            switch (chess.ArrowType)
            {
                case ChessmanButton.ChessmanArrowType.ONE:
                    ShowAbleEmptyBlock(chess.BlockLocation,chess.Deflection);
                    break;
                case ChessmanButton.ChessmanArrowType.TWO_45:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 45);

                    break;
                case ChessmanButton.ChessmanArrowType.TWO_90:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 90);
                    break;
                case ChessmanButton.ChessmanArrowType.TWO_135:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 135);
                    break;
                case ChessmanButton.ChessmanArrowType.TWO_180:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 180);
                    break;
                case ChessmanButton.ChessmanArrowType.THREE_45:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 45);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 90);

                    break;
                case ChessmanButton.ChessmanArrowType.THREE_90:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 90);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 180);

                    break;
                case ChessmanButton.ChessmanArrowType.FOUR:
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 90);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 180);
                    ShowAbleEmptyBlock(chess.BlockLocation, chess.Deflection + 270);
                    break;
                case ChessmanButton.ChessmanArrowType.JUMP:
                    ShowJumpAbleEmptyBlock(chess.BlockLocation, chess.Deflection);

                    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// 可走的格子遮罩层 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void target_block_Click(object sender, EventArgs e)
        {
            ChessBoardBlock block_panel = (ChessBoardBlock)sender;

            if (active_chess == null)
            {
                block_panel.Dispose();
                return;
            }

            if (IS_MOVE_LIMIT) // 如果有 不可连续两回合移动同一个棋子的规则
            {
                if (last_move_chess_dic.Keys.Contains(active_player)) //
                {
                    last_move_chess_dic[active_player].IsAbleMove = true;
                    last_move_chess_dic[active_player] = active_chess;
                    last_move_chess_dic[active_player].IsAbleMove = false;
                }
                else
                {
                    active_chess.IsAbleMove = false;
                    last_move_chess_dic.Add(active_player, active_chess);

                }
            }


            //     chess_panel.Controls.Remove(active_chess); // 棋盘中移除选中棋子
            checked_chess_panel.Visible = false; //取消选中棋子特效

            // BoardChess temp_chess = board_map_dic[active_chess.BlockLocation]; // 建立临时棋子储存当前棋子

            board_map_dic.Remove(active_chess.BlockLocation); // map中删除旧的棋子
            board_map_dic.Add(block_panel.BlockLocation, active_chess); // map中添加新的棋子

            
            active_chess.BlockLocation = block_panel.BlockLocation;// 改变选中棋子位置信息
            active_chess.Location = new Point(BORDER_WIDTH + BLOCK_SIZE * active_chess.BlockLocation.X + BLOCK_SIZE / 12,  // 注意，任何时候更改BlockLocation属性，也要同步更改Loacation属性
                                              BORDER_WIDTH + BLOCK_SIZE * active_chess.BlockLocation.Y + BLOCK_SIZE / 12);

            //   chess_panel.Controls.Add(active_chess); // 棋盘中添加新的棋子

         
            foreach(Point key in target_block_dic.Keys)
            {
                chess_panel.Controls.Remove(target_block_dic[key]);
                target_block_dic[key].Dispose();
            }
            //chess_panel.Controls.Remove(block_panel); // 删除目标格子特效
            //block_panel.Dispose();

            NextPlayer();
        }

        #endregion


        #region 菜单


        /// <summary>
        /// 玩法说明
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void game_illustrate_Click(object sender, EventArgs e)
        {
            FrmExplain frmExplain = new FrmExplain();
            frmExplain.Width = this.Width;
            frmExplain.Height = this.Height;
            frmExplain.Show();
        }


        /// <summary>
        /// 退出点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// 新开一局点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_new_game_Click(object sender, EventArgs e)
        {
            FrmNewGameSet frmNewGameSet = new FrmNewGameSet();
            frmNewGameSet.TopMost = true;
            frmNewGameSet.StartPosition = FormStartPosition.CenterScreen;
            frmNewGameSet.Show(this);
        }


        #endregion
    }
}
 